import { ShopItemInfo } from "../../resource/info/ShopItemInfo";
import { Cache } from "../../cache/Cache";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { PulseSharedObject } from "../player/PulseSharedObject";
import { Alert } from "../../../../com/mui/controls/Alert";
import { EPrictUnit } from "../../../../Message/Public/EPrictUnit";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { CheckBoxWin } from "../common/alertwins/CheckBoxWin";
import { Language } from "../../../../extend/language/Language";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { ItemData } from "../../resource/info/ItemData";
import { MsgManager } from "../../manager/MsgManager";
type int = number;
//class BuyAlert
    
    export  class BuyAlert
    {
        

        public static showByShopItemInfo(arg1: ShopItemInfo): void
        {
            var shopItemInfo: ShopItemInfo;
            var unit: int;
            var price: int;
            var totalPrice: int;
            var isAbundant: Boolean;
            var activeOffer: int;
            var amount: int;
            var unitName: string;
            var amountStr: string;
            var isNeedAlert: Boolean;
            var htmlContent: string;
            var onClickHandler: Function;

            var loc1;
            unit = 0;
            price = 0;
            totalPrice = 0;
            isAbundant = false;
            activeOffer = 0;
            amount = 0;
            unitName = null;
            amountStr = null;
            isNeedAlert = false;
            htmlContent = null;
            shopItemInfo = arg1;
            if (shopItemInfo) 
            {
                unit = shopItemInfo.unit;
                activeOffer = shopItemInfo.tshopSell.activeOffer;
                if (Cache.instance.shop.isShopMallDiscount && activeOffer > 0) 
                {
                    price = activeOffer;
                }
                else 
                {
                    price = shopItemInfo.tshopSell.offer == 0 ? shopItemInfo.tshopSell.price : shopItemInfo.tshopSell.offer;
                }
                totalPrice = price * shopItemInfo.buyNum;
                isAbundant = BuyAlert.checkMoney(unit, totalPrice);
                if (isAbundant) 
                {
                    amount = shopItemInfo.buyNum;
                    unitName = GameDefConfig.instance.getEPrictUnitName(unit);
                    amountStr = HTMLUtil.addColor(amount.toString(), "#FF0000");
                    isNeedAlert = !PulseSharedObject.isTodayNotTips("BuyAlert");
                    if (isNeedAlert) 
                    {
                        onClickHandler = (arg1: int, arg2: Boolean): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                if (unit != EPrictUnit._EPriceUnitMerit) 
                                {
                                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": shopItemInfo, "amount": amount}));
                                }
                                else 
                                {
                                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopBuyItem, {"itemInfo": shopItemInfo, "amount": amount}));
                                }
                                Alert.resetToDefault();
                            }
                            if (arg2) 
                            {
                                PulseSharedObject.save("BuyAlert", true);
                            }
                            return;
                        }
                        Alert.alertWinRenderer = CheckBoxWin;
                        if (unit != EPrictUnit._EPriceUnitMerit) 
                        {
                            htmlContent = Language.getStringByParam(54172, amountStr, ItemsUtil.getItemName(new ItemData(shopItemInfo.itemInfo.item.code)), totalPrice, unitName);
                        }
                        else 
                        {
                            htmlContent = Language.getStringByParam(54725, ItemsUtil.getItemName(new ItemData(shopItemInfo.itemInfo.item.code)));
                        }
                        BuyAlert._thisSprite = Alert.show(htmlContent, null, Alert.OK | Alert.CANCEL, null, onClickHandler);
                    }
                    else 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": shopItemInfo, "amount": amount}));
                    }
                }
            }
            return;
        }

        public static checkMoney(arg1: int, arg2: int, arg3: Boolean=false): Boolean
        {
            var unit: int;
            var totalPrice: int;
            var showBuyCoin: Boolean=false;
            var isAbundant: Boolean;
            var unitName: string;
            var isNeedAlert: Boolean;
            var onBtnClickHandler: Function;

            var loc1;
            isNeedAlert = false;
            unit = arg1;
            totalPrice = arg2;
            showBuyCoin = arg3;
            isAbundant = false;
            unitName = GameDefConfig.instance.getEPrictUnitName(unit);
            var loc2=unit;
            switch (loc2) 
            {
                case EPrictUnit._EPriceUnitCoin:
                {
                    if (Cache.instance.role.money.coin >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitCoinBind:
                {
                    if (Cache.instance.role.money.coinBind + Cache.instance.role.money.coin >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitGold:
                {
                    if (Cache.instance.role.money.gold >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitGoldBind:
                {
                    if (Cache.instance.role.money.goldBind + Cache.instance.role.money.gold >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitHonour:
                {
                    break;
                }
                case EPrictUnit._EPriceUnitArena:
                {
                    break;
                }
                case EPrictUnit._EPriceUnitPrestige:
                {
                    break;
                }
                case EPrictUnit._EPriceUnitPoint:
                {
                    if (Cache.instance.role.money.point >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitPetCoin:
                {
                    if (Cache.instance.role.money.petCoin >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitMerit:
                {
                    if (Cache.instance.role.money.merit >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitBrokenSoul:
                {
                    if (Cache.instance.role.money.brokenSoul >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitIntegralOne:
                {
                    if (Cache.instance.role.money.integralOne >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitIntegralTwo:
                {
                    if (Cache.instance.role.money.integralTwo >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitIntegralThree:
                {
                    if (Cache.instance.role.money.integralThree >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
                case EPrictUnit._EPriceUnitArenaCrossCoin:
                {
                    if (Cache.instance.role.money.arenaCrossCoin >= totalPrice) 
                    {
                        isAbundant = true;
                    }
                    break;
                }
            }
            if (isAbundant == false) 
            {
                if (unit == EPrictUnit._EPriceUnitGold || unit == EPrictUnit._EPriceUnitGoldBind) 
                {
                    isNeedAlert = !PulseSharedObject.isTodayNotTips("GotoPay");
                    if (isNeedAlert) 
                    {
                        onBtnClickHandler = (arg1: int, arg2: Boolean): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.GotoPay));
                                Alert.resetToDefault();
                            }
                            if (arg2) 
                            {
                                PulseSharedObject.save("GotoPay", true);
                            }
                            return;
                        }
                        Alert.alertWinRenderer = CheckBoxWin;
                        Alert.show(Language.getStringByParam(54173, unitName), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(54174, unitName));
                    }
                }
                else if (showBuyCoin) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, totalPrice));
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getStringByParam(54174, unitName));
                }
            }
            return isAbundant;
        }

        public static hide(): void
        {
            Alert.resetToDefault();
            if (BuyAlert._thisSprite && BuyAlert._thisSprite.parent) 
            {
                BuyAlert._thisSprite.parent.removeChild(BuyAlert._thisSprite);
            }
            return;
        }

        /* internal */ static /* var */_thisSprite: Object /* flash.display.Sprite */;
    }
